package com.fary.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.util.concurrent.ListenableFutureCallback;

@Slf4j
public class RabbitMQCorrelationData extends CorrelationData {

    public RabbitMQCorrelationData() {
        super.getFuture().addCallback(new RabbitMQListenableFutureCallback());
    }

    public RabbitMQCorrelationData(String id) {
        super(id);
        super.getFuture().addCallback(new RabbitMQListenableFutureCallback());
    }


    @Slf4j
    public static class RabbitMQListenableFutureCallback implements ListenableFutureCallback<Confirm> {

        @Override
        public void onFailure(Throwable ex) {
            // Future发生异常时的处理逻辑，基本不会触发
            log.error("send message fail", ex);
        }

        @Override
        public void onSuccess(Confirm result) {
            // Future接收到回执的处理逻辑，参数中的result就是回执内容
            if (result.isAck()) {
                // result.isAck()，boolean类型，true代表ack回执，false 代表 nack回执
                log.debug("发送消息成功，收到 ack!");
            } else {
                // result.getReason()，String类型，返回nack时的异常描述
                log.error("发送消息失败，收到 nack, reason : {}", result.getReason());
            }
        }
    }
}
